package com.acmen.easyapi.parse;

import com.acmen.easyapi.model.Cell;
import com.acmen.easyapi.model.ReqParamNode;
import com.acmen.easyapi.model.Table;
import com.acmen.easyapi.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author acmen
 * 详细表格输出执行器
 */
@Component
public class RequestTableParser {
    public List<Table> parseToTables(ReqParamNode reqParamNode){
        List<ReqParamNode> subNodes = reqParamNode.getSubNodes();
        List<Table> tables = new ArrayList<>();
        if(subNodes==null||subNodes.isEmpty()){
            return tables;
        }
        String title = reqParamNode.getLabel();
        if(reqParamNode.isArray()){
            title += "("+reqParamNode.getTypeAlias()+")";
        }
        Table table = new Table(UUID.randomUUID().toString(),title);
        tables.add(table);
        for(ReqParamNode node : subNodes){
            List<Cell> cells = new ArrayList<Cell>();
            List<Table> subTables = parseToTables(node);
            Cell cell = new Cell(node.getName());
            if (!subTables.isEmpty()) {
                tables.addAll(subTables);
                cell.setHref("#" + subTables.get(0).getId());
            }
            if(node.isDeprecated()){
                cell.setStyle("text-decoration:line-through;color:red;");
            }
            cells.add(cell);
            cells.add(new Cell(node.getLabel()));
            cells.add(new Cell(node.getTypeAlias()));
            cell = new Cell(node.isRequired() ? "是" : "否");
            if (node.isRequired()) {
                cell.setStyle("color:red");
            } else if (StringUtil.isNotEmpty(node.getRequiredGroup())) {
                cell.setStyle("color:red");
                cell.setValue("至少选一");
                cell.getContext().put("group", node.getRequiredGroup());
            }
            cells.add(cell);
            cells.add(new Cell(node.getMaxLength()));
            cells.add(new Cell(node.getMin()));
            cells.add(new Cell(node.getMax()));
            String example = "";
            if(StringUtil.isNotEmpty(node.getExample())){
                example = node.getExample().toString();
            }else if(StringUtil.isNotEmpty(node.getExample2())){
                example = node.getExample2().toString();
            }
            cell = new Cell(example);
            cell.setStyle("text-align:left");
            cells.add(cell);
            cell = new Cell(NoteBuilder.build(node.getNotes(),node.getDictionaries()));
            cell.setStyle("text-align:left");
            cells.add(cell);
            table.add(cells);
        }
        if (!table.getRows().isEmpty()) {
            mergeRequiredGroupColum(table.getRows(), 3);
        }
        return tables;
    }


    /**
     * 处理要必录的字段列
     *
     * @param rows
     */
    private void mergeRequiredGroupColum(List<List<Cell>> rows, int idx) {
        Collections.sort(rows, new Comparator<List<Cell>>() {
            @Override
            public int compare(List<Cell> o1, List<Cell> o2) {
                String group1 = o1.get(idx).getContext().get("group") + "";
                String group2 = o2.get(idx).getContext().get("group") + "";
                return group1.compareTo(group2);
            }
        });
        List<List<Cell>> rows_1 = rows.stream()
                .filter(c -> StringUtil.isNotEmpty(c.get(idx).getContext().get("group")))
                .collect(Collectors.toList());
        List<List<Cell>> rows_2 = rows.stream()
                .filter(c -> StringUtil.isEmpty(c.get(idx).getContext().get("group")))
                .collect(Collectors.toList());
        Cell flagCell = null;
        for (List<Cell> cells : rows_1) {
            if (flagCell == null) {
                flagCell = cells.get(idx);
            } else {
                if (flagCell.getContext().get("group").equals(cells.get(idx).getContext().get("group"))) {
                    cells.remove(idx);
                    flagCell.setRowspan(flagCell.getRowspan() + 1);
                } else {
                    flagCell = cells.get(idx);
                }
            }
        }
        rows.clear();
        rows.addAll(rows_1);
        rows.addAll(rows_2);
    }
}
